object InheritanceAndPolymorphismDemo extends App {
  import scala.collection.mutable

  abstract class CampusPerson(
                      val id: String,
                      var name: String,
                      var gender: String,
                      var contact: String
                    ) {
    def showBasicInfo(): Unit = {
      println(s"ID: $id")
      println(s"姓名: $name")
      println(s"性别: $gender")
      println(s"联系方式: $contact")
    }

    def work(): String
  }

  class Student(
                 id: String,
                 name: String,
                 gender: String,
                 contact: String,
                 var major: String,
                 var grade: Int
               ) extends CampusPerson(id, name, gender, contact) {

    private val completedCourses = mutable.Set.empty[String]

    def addCompletedCourse(courseId: String): Unit = {
      completedCourses += courseId
    }

    override def work(): String = {
      s"学生$name 正在学习$major 专业课程，已修${completedCourses.size}门课"
    }

    override def showBasicInfo(): Unit = {
      super.showBasicInfo()
      println(s"专业: $major")
      println(s"年级: $grade")
    }
  }

  class Teacher(
                 id: String,
                 name: String,
                 gender: String,
                 contact: String,
                 var title: String,
                 var department: String
               ) extends CampusPerson(id, name, gender, contact) {

    private val teachingCourses = mutable.Set.empty[String]

    def addTeachingCourse(courseId: String): Unit = {
      teachingCourses += courseId
    }

    override def work(): String = {
      s"教师$name（$title）在$department 系任教，教授${teachingCourses.size}门课"
    }

    override def showBasicInfo(): Unit = {
      super.showBasicInfo()
      println(s"职称: $title")
      println(s"部门: $department")
    }
  }

  class Staff(
               id: String,
               name: String,
               gender: String,
               contact: String,
               var position: String,
               var office: String
             ) extends CampusPerson(id, name, gender, contact) {

    override def work(): String = {
      s"行政人员$name 担任$position，在$office 办公"
    }

    override def showBasicInfo(): Unit = {
      super.showBasicInfo()
      println(s"职位: $position")
      println(s"办公室: $office")
    }
  }

  val people: List[CampusPerson] = List(
    new Student("S2025001", "张三", "男", "13800138000", "计算机", 2025),
    new Teacher("T1001", "李四", "女", "13900139000", "教授", "计算机"),
    new Staff("ST2001", "王五", "男", "13600136000", "行政主管", "办公楼201")
  )

  val student = people(0).asInstanceOf[Student]
  student.addCompletedCourse("C101")
  student.addCompletedCourse("C102")

  val teacher = people(1).asInstanceOf[Teacher]
  teacher.addTeachingCourse("C101")

  println("=== 校园人员工作状态 ===")
  people.foreach { person =>
    println(person.work())
  }

  println("\n=== 校园人员信息 ===")
  people.foreach { person =>
    println("-------------------")
    person.showBasicInfo()
  }

  println("\n=== 人员类型检查 ===")
  people.foreach { person =>
    val personType = person match {
      case _: Student => "学生"
      case _: Teacher => "教师"
      case _: Staff => "行政人员"
      case _ => "未知类型"
    }
    println(s"${person.name} 是 $personType")
  }
}
//11.1110【动手练习2】：实现校园人员管理系统